<?php
namespace App\Http\Controllers\Admin\Mall;


use App\Service\Admin\Mall\GoodsService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    //商城商品

    public function index()
    {
        $res = GoodsService::index();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //选项
    public function option()
    {
        $res = GoodsService::option();
        return $this->toJson(0, '', $res);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['shop_id'] = $request->post('shop_id', 0);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['price'] = $request->post('price', 0);
        $pas['price_old'] = $request->post('price_old', 0);
        $pas['detail'] = $request->post('detail', '');
        $icon = $request->post('img', '');
        $img01 = $request->post('img01', '');
        $img02 = $request->post('img02', '');
        $img03 = $request->post('img03', '');
        $img04 = $request->post('img04', '');
        if ($icon) {
            if (substr($icon, 0, 5) == 'data:') {
                $pas['cover'] = Upload::getPathByBase64($icon);
            } else {
                $pas['cover'] = $icon;
            }
        }
        $imgs = [];
        if ($img01) {
            if (substr($img01, 0, 5) == 'data:') {
                $imgs[] = Upload::getPathByBase64($img01);
            } else {
                $imgs[] = $icon;
            }
        }
        if ($img02) {
            if (substr($img02, 0, 5) == 'data:') {
                $imgs[] = Upload::getPathByBase64($img02);
            } else {
                $imgs[] = $icon;
            }
        }
        if ($img03) {
            if (substr($img03, 0, 5) == 'data:') {
                $imgs[] = Upload::getPathByBase64($img03);
            } else {
                $imgs[] = $icon;
            }
        }
        if ($img04) {
            if (substr($img04, 0, 5) == 'data:') {
                $imgs[] = Upload::getPathByBase64($img04);
            } else {
                $imgs[] = $icon;
            }
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '名称必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '分类必须的');
        }
        if (!$pas['shop_id']) {
            return $this->toJson(-2, '门店必须的');
        }
        if ($id) {
            $res = GoodsService::modify($id, $pas);
        } else {
            $res = GoodsService::add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $lastid = $res['data'];
        if (count($imgs)) {
            foreach ($imgs as $v) {
                GoodsService::addImgs($lastid, $v);
            }
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = GoodsService::del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}